package com.app.mymusic.service;

import android.app.Service;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.graphics.Point;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;

import com.app.mymusic.R;
import com.app.mymusic.ui.FullSreenActivity;

/**
 * Created by Administrator on 2016/7/26.
 */
public class FloatMusicService extends Service {
    private WindowManager mWindowManager;
    private WindowManager.LayoutParams mParams;
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        createFloatView();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        return super.onStartCommand(intent, flags, startId);
    }

    View view;
    private void createFloatView() {

        mWindowManager= (WindowManager) getApplication().getSystemService(getApplication().WINDOW_SERVICE);
        mParams = new WindowManager.LayoutParams();
        mParams.type= WindowManager.LayoutParams.TYPE_PHONE;
        //设置图片格式，效果为背景透明
        mParams.format = PixelFormat.RGBA_8888;
        //设置浮动窗口不可聚焦（实现操作除浮动窗口外的其他可见窗口的操作）
        mParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        mParams.gravity =  Gravity.RIGHT | Gravity.BOTTOM;
        //调整悬浮窗显示的停靠位置为左侧置顶wmParams.gravity = Gravity.LEFT | Gravity.TOP;
        // 以屏幕左上角为原点，设置x、y初始值，相对于gravity
//        mParams.x = 0;
//        mParams.y = 0;

        //设置悬浮窗口长宽数据
        mParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
        mParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        LayoutInflater mInflater=LayoutInflater.from(getApplicationContext());
        view=mInflater.inflate(R.layout.floatmusic,null);
        mWindowManager.addView(view,mParams);
        view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Point size=new Point();
                mWindowManager.getDefaultDisplay().getSize(size);
                int screenW=size.x;
                int screenH=size.y;
                mParams.x= (int) (screenW-event.getRawX()-view.getMeasuredWidth()/2);
                mParams.y= (int) (screenH-event.getRawY()-view.getMeasuredHeight()/2-25);
                mWindowManager.updateViewLayout(view,mParams);
                switch (event.getAction())
                {
                    case MotionEvent.ACTION_UP:
                    long t=event.getEventTime()-event.getDownTime();
                        if(t<500)
                        {
                            Intent i=new Intent();
                            i.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT|Intent.FLAG_ACTIVITY_NEW_TASK);
                            i.setClass(getApplicationContext(), FullSreenActivity.class);
                            startActivity(i);
                        }
                    break;
                }
                return false;
            }
        });
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        mWindowManager.removeView(view);
    }
}
